package dao

import (
	"behavior_srv/handler/models"
	"gorm.io/gorm"
)

// ClickBehaviorDAO 点击行为DAO
type ClickBehaviorDAO struct {
	DB *gorm.DB
}

// Create 创建单条点击行为记录
func (dao *ClickBehaviorDAO) Create(click *models.BehaviorClickRecords) error {
	return dao.DB.Create(click).Error
}

// BatchCreate 批量创建点击行为记录
func (dao *ClickBehaviorDAO) BatchCreate(clicks []*models.BehaviorClickRecords) error {
	return dao.DB.CreateInBatches(clicks, 100).Error
}

// GetByUserID 获取指定用户的点击行为记录
func (dao *ClickBehaviorDAO) GetByUserID(userID int64, limit, offset int) ([]*models.BehaviorClickRecords, error) {
	var clicks []*models.BehaviorClickRecords
	query := dao.DB.Where("user_id = ?", userID).Order("created_at DESC")
	if limit > 0 {
		query = query.Limit(limit).Offset(offset)
	}
	err := query.Find(&clicks).Error
	return clicks, err
}

// CountByUserID 统计指定用户的点击行为记录数量
func (dao *ClickBehaviorDAO) CountByUserID(userID int64) (int64, error) {
	var count int64
	err := dao.DB.Model(&models.BehaviorClickRecords{}).Where("user_id = ?", userID).Count(&count).Error
	return count, err
}

// GetByElementID 获取指定元素ID的点击行为记录
func (dao *ClickBehaviorDAO) GetByElementID(elementID string, limit, offset int) ([]*models.BehaviorClickRecords, error) {
	var clicks []*models.BehaviorClickRecords
	query := dao.DB.Where("element_id = ?", elementID).Order("created_at DESC")
	if limit > 0 {
		query = query.Limit(limit).Offset(offset)
	}
	err := query.Find(&clicks).Error
	return clicks, err
}

// GetByAction 获取指定操作类型的点击行为记录
func (dao *ClickBehaviorDAO) GetByAction(action string, limit, offset int) ([]*models.BehaviorClickRecords, error) {
	var clicks []*models.BehaviorClickRecords
	query := dao.DB.Where("action = ?", action).Order("created_at DESC")
	if limit > 0 {
		query = query.Limit(limit).Offset(offset)
	}
	err := query.Find(&clicks).Error
	return clicks, err
}
